﻿CREATE PROCEDURE [dbo].[proc_Project_Task_Create]
	(
		@FId uniqueidentifier,
		@OrderId uniqueidentifier,
		@ProjectId uniqueidentifier,
		@ProjectName nvarchar(50),
		@ProductId int,
		@CusName nvarchar(50),
		@Title nvarchar(50),
		@WebType smallint,
		@Description nvarchar(2000),
		@DownPerson nvarchar(50),
		@DownPersonName nvarchar(50),
		@ExecDepartmentId int,
		@ExecDepartmentName nvarchar(50),
		@ExecPerson nvarchar(50),
		@ExecPersonName nvarchar(50),
		@StartDate datetime,
		@IsFinish bit,
		@CreateDate datetime,
		@CompanyId int,
		@TypeId int
	)
AS
Begin Tran
	--下发人姓名
	Select @DownPersonName=EName From Employee Where UserName=@DownPerson
	
	Insert Into Project_Task
	(
		FId
		,OrderId
		,ProjectId
		,ProjectName
		,ProductId
		,CusName
		,Title
		,WebType
		,Description
		,DownPerson
		,DownPersonName
		,ExecDepartmentId
		,ExecDepartmentName
		,ExecPerson
		,ExecPersonName
		,StartDate
		,IsFinish
		,CreateDate
		,CompanyId
		,TypeId
	)
	Values
	(
		@FId
		,@OrderId
		,@ProjectId
		,@ProjectName
		,@ProductId
		,@CusName
		,@Title
		,@WebType
		,@Description
		,@DownPerson
		,@DownPersonName
		,@ExecDepartmentId
		,@ExecDepartmentName
		,@ExecPerson
		,@ExecPersonName
		,@StartDate
		,@IsFinish
		,@CreateDate
		,@CompanyId
		,@TypeId
	)

	--改变订单状态
	Update [Order] Set StateId=4 Where OId=@OrderId And StateId<4
	--改变项目完成状态(完成日期不会因为产生新任务而发生变化)
	Update Project Set IsFinish=0 Where ProjectId=@ProjectId

   If @@error <> 0  --发生错误
		Begin
			RollBack Tran
			Return
		End
    Else
		Begin
			Commit Tran
			Return    --执行成功
End